package modele.dao;

import java.sql.SQLException;
import java.util.ArrayList;

import modele.beans.Employee;
import modele.beans.EmployeeKey;
import modele.beans.Key;
import modele.database.IDatabase;

public class EmployeeKeyDAO {
	private IDatabase conn;
	
	public EmployeeKeyDAO(IDatabase conn) {
		this.conn = conn; 
	}
	
	public ArrayList<EmployeeKey> GetEmployeeKeys() throws SQLException, ClassNotFoundException {
		conn.Connect();
		ArrayList<ArrayList<Object>> employeeKeyList = conn.Select("SELECT KEY_ID, EMPLOYEE_ID FROM EMPLOYEE_KEY");
		
		ArrayList<EmployeeKey> employeeKeys = new ArrayList<EmployeeKey>();
		
		for (int i = 0; i != employeeKeyList.size(); ++i) {
			employeeKeys.add(new EmployeeKey(
					new Key("", "", employeeKeyList.get(i).get(0).toString()),
					new Employee("", "", "", "", employeeKeyList.get(i).get(1).toString())));
		}
		
		conn.Close();
		
		return employeeKeys;
	}
	
	public void AddEmployeeKey(EmployeeKey empKey) throws SQLException, ClassNotFoundException {
		conn.Connect();
		conn.Insert("INSERT INTO EMPLOYEE_KEY(KEY_ID, EMPLOYEE_ID) "
					+ " VALUES(?, ?)", empKey.getKey().getId(), empKey.getEmployee().getId());
		conn.Close();
	}
	
	public void RemoveEmployeeKey(EmployeeKey empKey) throws SQLException, ClassNotFoundException {
		conn.Connect();
		conn.Delete("DELETE FROM EMPLOYEE_KEY " +
					" WHERE KEY_ID = ? AND EMPLOYEE_ID = ?", empKey.getKey().getId(), empKey.getEmployee().getId());
		conn.Close();
	}
}
